﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Engine;

namespace Game
{
    public class XRecipeWidget : CanvasWidget
    {
        internal class Order
        {
            public Block block;

            public int order;

            public int value;

            public Order(Block b, int o, int v)
            {
                block = b;
                order = o;
                value = v;
            }
        }

        public ListPanelWidget m_blocksList;

        public ButtonWidget m_recipesButton;

        public ButtonWidget m_weaponButton;

        public ButtonWidget m_YaoshuiButton;

        public ButtonWidget m_GongyeButton;

        public ButtonWidget m_ClothesButton;

        public ButtonWidget m_ToolButton;

        public Screen m_previousScreen;

        public XSmeltingRecipeWidget m_smeltingRecipeWidget;

        public XCraftingRecipeWidget m_craftingRecipeWidget;

        public List<string> m_categories = new List<string>();

        public int m_categoryIndex;

        public int m_listCategoryIndex = -1;

        public ButtonWidget m_prevCategoryButton;

        public ButtonWidget m_nextCategoryButton;

        public ButtonWidget m_prevRecipeButton;

        public ButtonWidget m_nextRecipeButton;

        public int m_recipeIndex;

        public List<CraftingRecipe> m_craftingRecipes = new List<CraftingRecipe>();

        public XRecipeWidget()
        {
            XElement node = ContentManager.Get<XElement>("Widgets/XRecipeWidget");
            LoadContents(this, node);
            m_blocksList = Children.Find<ListPanelWidget>("BlocksList");
            m_smeltingRecipeWidget = Children.Find<XSmeltingRecipeWidget>("SmeltingRecipe");
            m_craftingRecipeWidget = Children.Find<XCraftingRecipeWidget>("CraftingRecipe");
            m_prevRecipeButton = Children.Find<ButtonWidget>("PreviousRecipe");
            m_nextRecipeButton = Children.Find<ButtonWidget>("NextRecipe");
            m_prevCategoryButton = Children.Find<ButtonWidget>("PreviousCategory");
            m_nextCategoryButton = Children.Find<ButtonWidget>("NextCategory");
            m_recipesButton = Children.Find<ButtonWidget>("RecipesButton");
            m_weaponButton = Children.Find<ButtonWidget>("WeaponButton");
            m_YaoshuiButton = Children.Find<ButtonWidget>("YaoshuiButton");
            m_GongyeButton = Children.Find<ButtonWidget>("GongyeButton");
            m_ClothesButton = Children.Find<ButtonWidget>("ClothesButton");
            m_ToolButton = Children.Find<ButtonWidget>("ToolButton");
            m_categories.Add(null);
            m_categories.AddRange(BlocksManager.Categories);
            m_blocksList.ItemWidgetFactory = delegate (object item)
            {
                int value2 = (int)item;
                int num = Terrain.ExtractContents(value2);
                Block block = BlocksManager.Blocks[num];
                XElement node2 = ContentManager.Get<XElement>("Widgets/RecipaediaItem");
                ContainerWidget containerWidget = (ContainerWidget)Widget.LoadWidget(this, node2, null);
                BlockIconWidget blockIconWidget = containerWidget.Children.Find<BlockIconWidget>("RecipaediaItem.Icon");
                blockIconWidget.Value = value2;
                blockIconWidget.Size = new Vector2(60f, 60f);
                LabelWidget labelWidget = containerWidget.Children.Find<LabelWidget>("RecipaediaItem.Text");
                labelWidget.Text = block.GetDisplayName(null, value2);
                labelWidget.FontScale = 0.6f;
                labelWidget.DropShadow = true;
                return containerWidget;
            };
            ListPanelWidget blocksList = m_blocksList;
            blocksList.ItemClicked = (Action<object>)Delegate.Combine(blocksList.ItemClicked, (Action<object>)delegate (object item)
            {
                int value = (int)item;
                m_recipeIndex = 0;
                m_craftingRecipes.Clear();
                m_craftingRecipes.AddRange(XCraftingRecipesManager.Recipes.Where((CraftingRecipe r) => r.ResultValue == value && r.ResultValue != 0));
            });
        }

        public override void Update()
        {
            if (m_listCategoryIndex != m_categoryIndex)
            {
                PopulateBlocksList();
            }

            m_prevCategoryButton.IsEnabled = m_categoryIndex > 0;
            m_nextCategoryButton.IsEnabled = m_categoryIndex < m_categories.Count - 1;
            int? value = null;
            int num = 0;
            if (m_blocksList.SelectedItem is int)
            {
                value = (int)m_blocksList.SelectedItem;
                num = XCraftingRecipesManager.Recipes.Count((CraftingRecipe r) => r.ResultValue == value);
            }

            if (m_recipesButton.IsClicked)
            {
                m_categoryIndex = 17;
            }

            if (m_ToolButton.IsClicked)
            {
                m_categoryIndex = 6;
            }

            if (m_weaponButton.IsClicked)
            {
                m_categoryIndex = 22;
            }

            if (m_YaoshuiButton.IsClicked)
            {
                m_categoryIndex = 21;
            }

            if (m_GongyeButton.IsClicked)
            {
                m_categoryIndex = 15;
            }

            if (m_ClothesButton.IsClicked)
            {
                m_categoryIndex = 7;
            }

            if (m_prevCategoryButton.IsClicked || base.Input.Left)
            {
                int num2 = m_categoryIndex - 1;
                if (num2 < 0)
                {
                    num2 = m_categories.Count - 1;
                }

                m_categoryIndex = num2;
            }

            if (m_nextCategoryButton.IsClicked || base.Input.Right)
            {
                int num3 = m_categoryIndex + 1;
                if (num3 > m_categories.Count - 1)
                {
                    num3 = 0;
                }

                m_categoryIndex = num3;
            }

            if (m_prevRecipeButton.IsClicked)
            {
                m_recipeIndex = MathUtils.Max(m_recipeIndex - 1, 0);
            }

            if (m_nextRecipeButton.IsClicked)
            {
                m_recipeIndex = MathUtils.Min(m_recipeIndex + 1, m_craftingRecipes.Count);
            }

            if (m_recipeIndex < m_craftingRecipes.Count)
            {
                CraftingRecipe craftingRecipe = m_craftingRecipes[m_recipeIndex];
                if (craftingRecipe.RequiredHeatLevel == 0f)
                {
                    m_craftingRecipeWidget.Recipe = craftingRecipe;
                    m_craftingRecipeWidget.NameSuffix = string.Format(LanguageControl.GetContentWidgets(GetType().Name, 1), m_recipeIndex + 1);
                    m_craftingRecipeWidget.IsVisible = true;
                    m_smeltingRecipeWidget.IsVisible = false;
                }
                else
                {
                    m_smeltingRecipeWidget.Recipe = craftingRecipe;
                    m_smeltingRecipeWidget.NameSuffix = string.Format(LanguageControl.GetContentWidgets(GetType().Name, 1), m_recipeIndex + 1);
                    m_smeltingRecipeWidget.IsVisible = true;
                    m_craftingRecipeWidget.IsVisible = false;
                }
            }
        }

        public void PopulateBlocksList()
        {
            m_listCategoryIndex = m_categoryIndex;
            string text = m_categories[m_categoryIndex];
            m_blocksList.ScrollPosition = 0f;
            m_blocksList.ClearItems();
            List<Order> list = new List<Order>();
            Block[] blocks = BlocksManager.Blocks;
            foreach (Block block in blocks)
            {
                foreach (int creativeValue in block.GetCreativeValues())
                {
                    if (string.IsNullOrEmpty(text) || block.GetCategory(creativeValue) == text)
                    {
                        list.Add(new Order(block, block.GetDisplayOrder(creativeValue), creativeValue));
                    }
                }
            }

            IOrderedEnumerable<Order> orderedEnumerable = list.OrderBy((Order o) => o.order);
            foreach (Order item in orderedEnumerable)
            {
                m_blocksList.AddItem(item.value);
            }
        }
    }
}